package com.testdemo.common.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.testdemo.MyApplication;


/**
 * Created by joson on 2017/2/15.
 * SharedPreferences存取工具（包括用户个人数据与公共数据）
 */
public class SpUtil {




    /**
     * 保存个人String
     *
     * @param key
     * @param value
     */
    /*public static void putPersonalString(String key, String value) {
        String fKey = formatPersonalKey(key);
        SharedPreferences sp = MyApplication.myApplication.getSharedPreferences(fKey, Context.MODE_PRIVATE);
        sp.edit().putString(fKey, value).commit();
    }*/

    /**
     * 获取个人String
     *
     * @param key
     */
    /*public static String getPersonalString(String key) {
        return getPersonalString(key, "");
    }
*/
    /**
     * 获取个人String
     *
     * @param key
     */
    /*public static String getPersonalString(String key,String defaultValue) {
        String fKey = formatPersonalKey(key);
        SharedPreferences sp = MyApplication.myApplication.getSharedPreferences(fKey, Context.MODE_PRIVATE);
        return sp.getString(fKey, defaultValue);
    }
*/
    /**
     * 保存个人long
     *
     * @param key
     * @param value
     */
    /*public static void putPersonalLong(String key, long value) {
        String fKey = formatPersonalKey(key);
        SharedPreferences sp = MyApplication.myApplication.getSharedPreferences(fKey, Context.MODE_PRIVATE);
        sp.edit().putLong(fKey, value).commit();
    }*/

    /**
     * 获取个人long
     *
     * @param key
     */
    /*public static long getPersonalLong(String key, int defValue) {
        String fKey = formatPersonalKey(key);
        SharedPreferences sp = MyApplication.myApplication.getSharedPreferences(fKey, Context.MODE_PRIVATE);
        return sp.getLong(fKey, defValue);
    }*/

    /**
     * 保存个人Int
     *
     * @param key
     * @param value
     */
    /*public static void putPersonalInt(String key, int value) {
        String fKey = formatPersonalKey(key);
        SharedPreferences sp = MyApplication.myApplication.getSharedPreferences(fKey, Context.MODE_PRIVATE);
        sp.edit().putInt(fKey, value).commit();
    }*/

    /**
     * 获取个人int
     *
     * @param key
     */
    /*public static int getPersonalInt(String key, int defValue) {
        String fKey = formatPersonalKey(key);
        SharedPreferences sp = MyApplication.myApplication.getSharedPreferences(fKey, Context.MODE_PRIVATE);
        return sp.getInt(fKey, defValue);
    }*/

    /**
     * 保存个人Boolean
     *
     * @param key
     * @param value
     */
    /*public static void putPersonalBoolean(String key, boolean value) {
        String fKey = formatPersonalKey(key);
        SharedPreferences sp = MyApplication.myApplication.getSharedPreferences(fKey, Context.MODE_PRIVATE);
        sp.edit().putBoolean(fKey, value).commit();
    }*/

    /**
     * 获取个人Boolean
     *
     * @param key
     *//*
    public static Boolean getPersonalBoolean(String key, boolean defValue) {
        String fKey = formatPersonalKey(key);
        SharedPreferences sp = MyApplication.myApplication.getSharedPreferences(fKey, Context.MODE_PRIVATE);
        return sp.getBoolean(fKey, defValue);
    }*/

    /**
     * 保存公共String
     *
     * @param key
     * @param value
     */
    public static void putPublicString(String key, String value) {
        String fKey = formatPublicKey(key);
        SharedPreferences sp = MyApplication.myApplication.getSharedPreferences(fKey, Context.MODE_PRIVATE);
        sp.edit().putString(fKey, value).commit();
    }

    /**
     * 获取公共String
     *
     * @param key
     */
    public static String getPublicString(String key) {
        return getPublicString(key,"");
    }
    public static String getPublicString(String key,String dufaultValue) {
        String fKey = formatPublicKey(key);
        SharedPreferences sp = MyApplication.myApplication.getSharedPreferences(fKey, Context.MODE_PRIVATE);
        return sp.getString(fKey, dufaultValue);
    }


    /**
     * 保存公共Boolean
     *
     * @param key
     * @param value
     */
    public static void putPublicBoolean(String key, boolean value) {
        String fKey = formatPublicKey(key);
        SharedPreferences sp = MyApplication.myApplication.getSharedPreferences(fKey, Context.MODE_PRIVATE);
        sp.edit().putBoolean(fKey, value).commit();
    }

    /**
     * 获取公共Boolean
     *
     * @param key
     */
    public static Boolean getPublicBoolean(String key, boolean defValue) {
        String fKey = formatPublicKey(key);
        SharedPreferences sp = MyApplication.myApplication.getSharedPreferences(fKey, Context.MODE_PRIVATE);
        return sp.getBoolean(fKey, defValue);
    }

    /**
     * 保存公共long
     *
     * @param key
     * @param value
     */
    public static void putPublicLong(String key, long value) {
        String fKey = formatPublicKey(key);
        SharedPreferences sp = MyApplication.myApplication.getSharedPreferences(fKey, Context.MODE_PRIVATE);
        sp.edit().putLong(fKey, value).commit();
    }

    /**
     * 获取公共Long
     *
     * @param key
     */
    public static long getPublicLong(String key, long defValue) {
        String fKey = formatPublicKey(key);
        SharedPreferences sp = MyApplication.myApplication.getSharedPreferences(fKey, Context.MODE_PRIVATE);
        return sp.getLong(fKey, defValue);
    }

    /**
     * 保存公共Int
     *
     * @param key
     * @param value
     */
    public static void putPublicInt(String key, int value) {
        String fKey = formatPublicKey(key);
        SharedPreferences sp = MyApplication.myApplication.getSharedPreferences(fKey, Context.MODE_PRIVATE);
        sp.edit().putInt(fKey, value).commit();
    }

    /**
     * 获取公共Int
     *
     * @param key
     */
    public static int getPublicInt(String key, int defValue) {
        String fKey = formatPublicKey(key);
        SharedPreferences sp = MyApplication.myApplication.getSharedPreferences(fKey, Context.MODE_PRIVATE);
        return sp.getInt(fKey, defValue);
    }


    public static void removeKey(String key) {
        String fKey = formatPublicKey(key);
        SharedPreferences sp = MyApplication.myApplication.getSharedPreferences(fKey, Context.MODE_PRIVATE);
        sp.edit().remove(fKey).commit();
    }


    /**
     * 将Key格式化
     *//*
    private static String formatPersonalKey(String key) {
        return MD5Util.string2MD5(UserInfoUtil.getUserName() + "_sp_private_" + key);
    }
*/
    /**
     * 将Key格式化
     */
    private static String formatPublicKey(String key) {
        return MD5Util.string2MD5("_sp_public_" + key);
    }
}
